var http = require('http');
var express = require('express');
var path = require('path');

var favicon = require('serve-favicon');
var cors = cors = require('cors');
var logger = require('morgan');
var methodOverride = require('method-override');
var session = require('express-session');
var bodyParser = require('body-parser');
var multer = require('multer');
var errorHandler = require('errorhandler');
var app = express();
// 环境
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(cors());
app.use(logger('dev'));
app.use(methodOverride());
app.use(session({ resave: true,
                  saveUninitialized: true,
                  secret: 'uwotm8' }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer());
app.use(express.static(path.join(__dirname, 'public')));


// 加载路由完成后才能加载错误处理中间件
if ('development' == app.get('env')) {
  app.use(errorHandler());
}


var server = http.createServer(app);
server.listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

var io = require('socket.io')(server);

var int = 1;
io.on('connection', function (socket) {
  var page = 1;
  socket.on("news",function(d){
    page = d.page;
  });
  setInterval(function(){
    int ++;
    var getData = function(page,str){
      if(page == 1){
        return [{
              "code": "0000",
              "data": [{
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 17800,
                    "begintime": str,
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600803",
                    "createtime": "2016-09-30 10:47:39",
                    "endtime": "2016-09-30 10:47:39",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 23138,
                    "lastupdate": "2016-09-30 10:48:02",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 11.2,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 5300,
                    "begintime": str,
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600801",
                    "createtime": "2016-09-30 10:47:39",
                    "endtime": "2016-09-30 10:47:39",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 23115,
                    "lastupdate": "2016-09-30 10:48:01",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 7.02,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 4100,
                    "begintime": str,
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600158",
                    "createtime": "2016-09-30 10:47:24",
                    "endtime": "2016-09-30 10:47:24",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 22226,
                    "lastupdate": "2016-09-30 10:47:43",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 16.01,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 5300,
                    "begintime": "2016-09-30 10:42:36",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600801",
                    "createtime": "2016-09-30 10:42:36",
                    "endtime": "2016-09-30 10:42:36",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 21930,
                    "lastupdate": "2016-09-30 10:44:16",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 7.03,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 4100,
                    "begintime": "2016-09-30 10:42:24",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600158",
                    "createtime": "2016-09-30 10:42:24",
                    "endtime": "2016-09-30 10:42:24",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 21249,
                    "lastupdate": "2016-09-30 10:43:56",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 16.01,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 6500,
                    "begintime": "2016-09-30 10:33:00",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "300313",
                    "createtime": "2016-09-30 10:33:00",
                    "endtime": "2016-09-30 10:33:00",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 20880,
                    "lastupdate": "2016-09-30 10:33:21",
                    "market": "SZ",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 16.68,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 17800,
                    "begintime": "2016-09-30 10:32:39",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600803",
                    "createtime": "2016-09-30 10:32:39",
                    "endtime": "2016-09-30 10:32:39",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 20467,
                    "lastupdate": "2016-09-30 10:33:08",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 11.15,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 5300,
                    "begintime": "2016-09-30 10:32:35",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600801",
                    "createtime": "2016-09-30 10:32:35",
                    "endtime": "2016-09-30 10:32:35",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 20143,
                    "lastupdate": "2016-09-30 10:33:02",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 7.02,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 4100,
                    "begintime": "2016-09-30 10:32:24",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600158",
                    "createtime": "2016-09-30 10:32:24",
                    "endtime": "2016-09-30 10:32:24",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 19371,
                    "lastupdate": "2016-09-30 10:32:42",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 16.04,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 6500,
                    "begintime": "2016-09-30 10:27:54",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "300313",
                    "createtime": "2016-09-30 10:27:54",
                    "endtime": "2016-09-30 10:27:54",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 19046,
                    "lastupdate": "2016-09-30 10:28:06",
                    "market": "SZ",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 16.66,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                }
            ],
            "msg": "",
            "msgType": "me_signal_order",
            "page": {
                "begin": 0,
                "count": true,
                "currentPage": 1,
                "currentPageRows": 10,
                "end": 10,
                "first": true,
                "last": false,
                "length": 10,
                "totalPage": 6,
                "totalRows": 51
            }
        }]
      }
      else{
        return [{
                "code": "0000",
                "data": [{
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 17800,
                    "begintime": "2016-09-30 10:47:39",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600803",
                    "createtime": "2016-09-30 10:47:39",
                    "endtime": "2016-09-30 10:47:39",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 23138,
                    "lastupdate": "2016-09-30 10:48:02",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 11.2,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 5300,
                    "begintime": "2016-09-30 10:47:39",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600801",
                    "createtime": "2016-09-30 10:47:39",
                    "endtime": "2016-09-30 10:47:39",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 23115,
                    "lastupdate": "2016-09-30 10:48:01",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 7.02,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 4100,
                    "begintime": str,
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600158",
                    "createtime": "2016-09-30 10:47:24",
                    "endtime": "2016-09-30 10:47:24",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 22226,
                    "lastupdate": "2016-09-30 10:47:43",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 16.01,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 5300,
                    "begintime": "2016-09-30 10:42:36",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600801",
                    "createtime": "2016-09-30 10:42:36",
                    "endtime": "2016-09-30 10:42:36",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 21930,
                    "lastupdate": "2016-09-30 10:44:16",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 7.03,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 4100,
                    "begintime": "2016-09-30 10:42:24",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600158",
                    "createtime": "2016-09-30 10:42:24",
                    "endtime": "2016-09-30 10:42:24",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 21249,
                    "lastupdate": "2016-09-30 10:43:56",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 16.01,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 6500,
                    "begintime": "2016-09-30 10:33:00",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "300313",
                    "createtime": "2016-09-30 10:33:00",
                    "endtime": "2016-09-30 10:33:00",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 20880,
                    "lastupdate": "2016-09-30 10:33:21",
                    "market": "SZ",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 16.68,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                },
                {
                    "accountid": "110011",
                    "algoName": "DIRECT",
                    "amount": 17800,
                    "begintime": "2016-09-30 10:32:39",
                    "brokerid": "001",
                    "bstype": "S",
                    "canceltimeout": 60,
                    "ccy": "CNY",
                    "code": "600803",
                    "createtime": "2016-09-30 10:32:39",
                    "endtime": "2016-09-30 10:32:39",
                    "finishAmount": 0,
                    "frozenFinCapital": 0,
                    "fundFree": 0,
                    "fundFrozen": 0,
                    "hedgeflag": "1",
                    "id": 20467,
                    "lastupdate": "2016-09-30 10:33:08",
                    "market": "SH",
                    "minOrderamount": 25000,
                    "offsetflag": "C",
                    "preId": 0,
                    "priceCurr": 11.15,
                    "priceHigh": 0,
                    "priceLower": 0,
                    "sendAmount": 0,
                    "server": "algo1",
                    "signalid": 0,
                    "status": 9,
                    "strategyid": 257,
                    "tradeonlimit": 0,
                    "userid": "cqtl001",
                    "price": int
                }
            ],
            "msg": "",
            "msgType": "me_signal_order",
            "page": {
                "begin": 0,
                "count": true,
                "currentPage": 1,
                "currentPageRows": 10,
                "end": 10,
                "first": true,
                "last": false,
                "length": 10,
                "totalPage": 6,
                "totalRows": 51
            }
        }]
      }
    }
    var sendData = "";
    var d = new Date();
    var str = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate() + "-" + d.getHours() + "-" + d.getMinutes() + "-" + d.getSeconds();
    socket.emit(
      'news', getData(page,str)
    );
  },200);
});
